package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.classes.ClassesTransfer;
import com.admin.education.dataTransferObject.classes.Request.CreateClassesRequest;
import com.admin.education.dataTransferObject.classes.Request.QueryClassesRequest;
import com.admin.education.dataTransferObject.classes.Request.UpdateClassesRequest;
import com.admin.education.services.Interfaces.IClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController
@RequestMapping("classes")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ClassesController {
    @Autowired
    private IClassService classService;
    @GetMapping("")
    public Callable<IResponseMessage> queryClasses(@Valid QueryClassesRequest queryClassesRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<ClassesTransfer>> servicesExcuteResult = classService.queryClasses(queryClassesRequest);

                if (servicesExcuteResult.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<PageResponse<ClassesTransfer>>(Result.Success, servicesExcuteResult.getData());
                } else {
                    throw new BadRequestException(servicesExcuteResult.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @GetMapping("{classesId}")
    public Callable<IResponseMessage> queryClassesById(@PathVariable Long classesId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<ClassesTransfer> result = classService.queryClassesById(classesId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessageWithData<ClassesTransfer>(Result.Success, result.getData());
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PostMapping("")
    public Callable<IResponseMessage> createClasses(@RequestBody CreateClassesRequest createClassesRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = classService.createClasses(createClassesRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @DeleteMapping("{classesId}")
    public Callable<IResponseMessage> deleteClassesById(@PathVariable Long classesId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = classService.deleteClasses(classesId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{classesId}")
    public Callable<IResponseMessage> updateClasses(@PathVariable Long classesId, @RequestBody @Valid UpdateClassesRequest updateClassesRequest) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = classService.updateClasses(classesId, updateClassesRequest);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }

    @PutMapping("{classesId}/Valid")
    public Callable<IResponseMessage> updateClassesValid(@PathVariable Long classesId) {
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = classService.updateClassesValid(classesId);

                if (result.isSuccess()) {
                    responseMessage = new ResponseMessage(Result.Success);
                    ;
                } else {
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
